﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pb;
using cn.sharesdk.unity3d;

public class GamePDKPanel : MonoBehaviour {

    EginPDK pdk = EginPDK.Instance;

    public GameObject LeaveBtn;
    public GameObject DelegateBtn;
    public GameObject SetBtn;
    public GameObject ChatBtn; 
    public GameObject ChangeTableOrangeBtn;
    public GameObject ChangeTableGrayBtn;
    public GameObject PrepareGreenBtn;
    public GameObject PrepareGrayBtn;
    public GameObject ShowSpriteBtn;
    public GameObject HideSpriteBtn;
    public GameObject InviteOrangeBtn;
    public GameObject CopyRoomIdBtn;

    public GameObject FriendRoomObj;
    public GameObject LeaveAndDelegate;

    public List<UISprite> roomIdSprites;
    private ShareSDK ssdk;

	void Awake () {
        ssdk = GameObject.Find("SoundManger").GetComponent<ShareSDK>();
        ssdk.shareHandler = OnShareResultHandler;


        UIEventListener.Get(LeaveBtn).onClick = ButtonClick;
        UIEventListener.Get(DelegateBtn).onClick = ButtonClick;
        UIEventListener.Get(SetBtn).onClick = ButtonClick;
        UIEventListener.Get(ChatBtn).onClick = ButtonClick;       
        UIEventListener.Get(ChangeTableOrangeBtn).onClick = ButtonClick;
        UIEventListener.Get(ChangeTableGrayBtn).onClick = ButtonClick;
        UIEventListener.Get(PrepareGreenBtn).onClick = ButtonClick;
        UIEventListener.Get(PrepareGrayBtn).onClick = ButtonClick;
        UIEventListener.Get(ShowSpriteBtn).onClick = ButtonClick;
        UIEventListener.Get(HideSpriteBtn).onClick = ButtonClick;
        UIEventListener.Get(InviteOrangeBtn).onClick = ButtonClick;
        UIEventListener.Get(CopyRoomIdBtn).onClick = ButtonClick;
       
	}

    void ButtonClick(GameObject button)
    {
        if (button.name == "LeaveBtn")
        {           
                //解散房间
            if (pdk.createrUid == int.Parse(EginUser.Instance.Uid))
            {
                Debug.Log("解散房间1");
                if (pdk.isAgentRoom == false)
                {
                    Debug.Log("解散房间2");
                    RoomDismissRequest rl = new RoomDismissRequest { RoomId = pdk.roomId };
                    SocketManager socketManager = SocketManager.Instance;
                    socketManager.SendProtobuf1(rl, MsgId.RoomDismissRequest);
                }
            }           
            //离开房间
            else
            {
                Debug.Log("离开房间1");
                RoomLeaveRequest rl = new RoomLeaveRequest { RoomId = pdk.roomId };
                SocketManager socketManager = SocketManager.Instance;
                socketManager.SendProtobuf1(rl, MsgId.RoomLeaveRequest);
            }
           
        }
        else if (button.name == "DelegateBtn")
        {
            //托管
            if(pdk.roomStatus == ROOM_STATUS.Gaming){
                UserTurnOnRobotRequest ut = new UserTurnOnRobotRequest { };
                SocketManager socketManager = SocketManager.Instance;
                socketManager.SendProtobuf1(ut, MsgId.UserTurnOnRobotRequest);
            }           
        }
        else if (button.name == "SetBtn")
        {
            //设置
            GameObject Set = NGUITools.AddChild(GameObject.Find("UI Root"),(GameObject)Resources.Load(GamePDKPrefabPath.SetPanel));
        }
        else if (button.name == "ChatBtn")
        {
            //聊天
            GameObject Set = NGUITools.AddChild(GameObject.Find("UI Root"), (GameObject)Resources.Load(GamePDKPrefabPath.ChatPanel));
        }
        else if (button.name == "ChangeTableOrangeBtn")
        {
            //换桌
            RunChangeTableRequest rc = new RunChangeTableRequest { };
            SocketManager socketManager = SocketManager.Instance;
            socketManager.SendProtobuf1(rc,MsgId.RunChangeTableRequest);
        }
        else if (button.name == "InviteOrangeBtn")
        {
            //邀请
            string tempRoomId = "";
            for (int i = 0; i < roomIdSprites.Count;i++ )
            {
                tempRoomId = tempRoomId + int.Parse(roomIdSprites[i].spriteName);
            }

            Debug.Log("分享给微信好友");
            ShareContent content = new ShareContent();
            string downloadUrl = GameObject.Find("UI Root/HallPanel").GetComponent<HallModel>().downloadUrl;
            content.SetText("快来和我一起游戏吧！房间号：" + tempRoomId);
            //content.SetImageUrl("https://f1.webshare.mob.com/code/demo/img/1.jpg");
            content.SetTitle("欢乐掌上棋牌");
            //content.SetTitleUrl("http://www.mob.com");
            content.SetSite("Mob-ShareSDK");
            content.SetSiteUrl("http://www.mob.com");
            content.SetUrl(downloadUrl);
            content.SetComment("欢乐嗨翻天！");
            //content.SetMusicUrl("http://mp3.mwap8.com/destdir/Music/2009/20090601/ZuiXuanMinZuFeng20090601119.mp3");
            content.SetShareType(ContentType.Text);
            ssdk.ShareContent(PlatformType.WeChat, content);

        }
        else if (button.name == "CopyRoomIdBtn")
        {
            //复制房间号
            string roomIdString = "";
            for (int i = 0; i < roomIdSprites.Count;i++ )
            {
                roomIdString += roomIdSprites[i].spriteName;
            }
            UniPasteBoard.SetClipBoardString(roomIdString); 
        }
        else if (button.name == "PrepareGreenBtn")
        {
            //准备
            RoomReadyRequest rr = new RoomReadyRequest { RoomId = pdk.roomId};
            SocketManager socketManager = SocketManager.Instance;
            socketManager.SendProtobuf1(rr,MsgId.RoomReadyRequest);
        }       
        else if (button.name == "ShowSpriteBtn")
        {
            LeaveAndDelegate.SetActive(true);
            ShowSpriteBtn.SetActive(false);
            HideSpriteBtn.SetActive(true);
        }
        else if (button.name == "HideSpriteBtn")
        {
            LeaveAndDelegate.SetActive(false);
            ShowSpriteBtn.SetActive(true);
            HideSpriteBtn.SetActive(false);
        }
        else if (button.name == "CancelAutoSatusBtn")
        {
            UserCancelRobotRequest uc = new UserCancelRobotRequest { };
            SocketManager socketManager = SocketManager.Instance;
            socketManager.SendProtobuf1(uc,MsgId.UserCancelRobotRequest);
        }
    }

    void OnShareResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable result)
    {
        if (state == ResponseState.Success)
        {
            print("share successfully - share result :");
            print(MiniJSON.jsonEncode(result));
        }
        else if (state == ResponseState.Fail)
        {
#if UNITY_ANDROID
            print("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
#elif UNITY_IPHONE
			print ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
#endif
        }
        else if (state == ResponseState.Cancel)
        {
            print("cancel !");
        }
    }

}
